package com.zh.webService.impl;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Component;

import com.zh.model.User;
import com.zh.webService.MyWebServiceTest;

//@Component("myWebServiceTestImpl")
@Component("myWebServiceTestImpl")
public class MyWebServiceTestImpl implements MyWebServiceTest {
	
	@Resource(name = "messageSourceAccessor")
	private  MessageSourceAccessor mr;

	public String queryParam(String name) {
		// TODO Auto-generated method stub
		System.out.println("WebServiceImpl-->queryParam-->name:"+name);
		

		User u=new User();
		u.setId("111");
		u.setName("你好");
		u.setInfo("infoinfoinfo");
		
		if("".equals(name)){
			u.setName(name);
		}
		
		String user = null;
		
		
		ObjectMapper mapper = new ObjectMapper();
		
		
		try {
			user=mapper.writeValueAsString(u);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return user;
	}

	@Override
	public String pathParam(String name) {
		
		System.out.println("WebServiceImpl-->pathParam-->name:"+name);
		//使用spring配置读取属性文件
		String ip=mr.getMessage("serverIp.srpIp");
		String port=mr.getMessage("serverIp.srpPort");
		System.out.println("ip="+ip+",port="+port);
		String dburl=mr.getMessage("jdbc.url");
		System.out.println(dburl);
		
		//手动读取属性文件
		Message message = PhaseInterceptorChain.getCurrentMessage();
		HttpServletRequest request = (HttpServletRequest) message
				.get(AbstractHTTPDestination.HTTP_REQUEST);
		InputStream in = request.getSession().getServletContext().getResourceAsStream("/WEB-INF/classes/serverIp.properties");
		Properties pro = new Properties();
		String val = "";
		try {
			pro.load(in);
			in.close();
			val = pro.getProperty("serverIp.srpIp");
			System.out.println(val);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		User u=new User();
		u.setId("111");
		u.setName("你好");
		u.setInfo("infoinfoinfo");
		
		if(!"".equals(name)){
			u.setName(name);
		}
		
		String user = null;
		
		
		ObjectMapper mapper = new ObjectMapper();
		
		
		try {
			user=mapper.writeValueAsString(u);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return user;
		
	}
	


	//POST

	@Override
	public String nameInfo(String name) {
		// TODO Auto-generated method stub
		System.out.println("WebServiceImpl-->post --> nameInfo-->name:"
				+ name);
		
		User u=new User();
		u.setId("111");
		u.setName("你好");
		u.setInfo("infoinfoinfo");
		
		if(!("".equals(name)||name==null)){
			u.setName(name);
		}
		
		String user = null;
		
		
		ObjectMapper mapper = new ObjectMapper();
		
		
		try {
			user=mapper.writeValueAsString(u);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return user;
	}
	
	
	public String allInfo(String info){
		
		System.out.println("WebServiceImpl-->post --> allInfo-->info:"+info);
		
		return "{\"status\":0}";
	}

	@Override
	public String userInfo(User user) {
		// TODO Auto-generated method stub
		
		System.out.println("WebServiceImpl-->post --> modelInfo-->user:"+user);
		
      ObjectMapper mapper = new ObjectMapper();
		
		String str="";
		try {
			str=mapper.writeValueAsString(user);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		
		return str;
	}


	
	
	
	
	
	
  
	// end
}
